/**
 * 
 */
package tk.redbluelotto.service;

import java.util.HashMap;
import java.util.Map;

import tk.redbluelotto.model.RedBall;
import tk.redbluelotto.util.CalculateUtil;

/**
 * 上次值规则LAST_1=1:5,LAST_2=1:5
 * 
 * @author zb
 * 
 */
public class LastValueRule implements IRule {

	private long lastValue[];

	private Map<String, Integer[]> ruleMap = new HashMap<String, Integer[]>(2);

	private long time = 0;

	public LastValueRule(String ruleExpress) {
		init(ruleExpress);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rbl.IRule#init(java.lang.String)
	 */
	@Override
	public void init(String ruleExpress) {
		lastValue = FetchLastNumber.getLastValue(10);
		String[] rules = ruleExpress.split(",");
		for (String rule : rules) {
			String[] operData = rule.split("=");
			String[] dataStrings = operData[1].split(":");
			ruleMap.put(operData[0], CalculateUtil.str2Int(dataStrings));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.rbl.IRule#isSatisfied(java.lang.Long)
	 */
	@Override
	public boolean isSatisfied(RedBall number) {
		for (String oper : ruleMap.keySet()) {
			for (int i = 0; i < ruleMap.get(oper).length - 1; i++) {
				if (Long.bitCount(lastValue[Integer.valueOf(oper.substring(5, oper.length())) - 1] & number.idx) != ruleMap.get(oper)[i]) {
					return false;
				}
			}
		}
		return true;
	}

	public long getTime() {
		return time;
	}

}
